{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eb8aaa5b-a3fd-4f18-b5a5-fbbc87f73b8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "葡萄酒数据集如下：\n",
      "     label     a1    a2    a3    a4   a5    a6    a7    a8    a9    a10   a11  \\\n",
      "0        1  14.23  1.71  2.43  15.6  127  2.80  3.06  0.28  2.29   5.64  1.04   \n",
      "1        1  13.20  1.78  2.14  11.2  100  2.65  2.76  0.26  1.28   4.38  1.05   \n",
      "2        1  13.16  2.36  2.67  18.6  101  2.80  3.24  0.30  2.81   5.68  1.03   \n",
      "3        1  14.37  1.95  2.50  16.8  113  3.85  3.49  0.24  2.18   7.80  0.86   \n",
      "4        1  13.24  2.59  2.87  21.0  118  2.80  2.69  0.39  1.82   4.32  1.04   \n",
      "..     ...    ...   ...   ...   ...  ...   ...   ...   ...   ...    ...   ...   \n",
      "173      3  13.71  5.65  2.45  20.5   95  1.68  0.61  0.52  1.06   7.70  0.64   \n",
      "174      3  13.40  3.91  2.48  23.0  102  1.80  0.75  0.43  1.41   7.30  0.70   \n",
      "175      3  13.27  4.28  2.26  20.0  120  1.59  0.69  0.43  1.35  10.20  0.59   \n",
      "176      3  13.17  2.59  2.37  20.0  120  1.65  0.68  0.53  1.46   9.30  0.60   \n",
      "177      3  14.13  4.10  2.74  24.5   96  2.05  0.76  0.56  1.35   9.20  0.61   \n",
      "\n",
      "      a12   a13  \n",
      "0    3.92  1065  \n",
      "1    3.40  1050  \n",
      "2    3.17  1185  \n",
      "3    3.45  1480  \n",
      "4    2.93   735  \n",
      "..    ...   ...  \n",
      "173  1.74   740  \n",
      "174  1.56   750  \n",
      "175  1.56   835  \n",
      "176  1.62   840  \n",
      "177  1.60   560  \n",
      "\n",
      "[178 rows x 14 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "names=['label','a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','a11','a12','a13']\n",
    "dataset=pd.read_csv(\"wine.data\",names=names)\n",
    "print(\"葡萄酒数据集如下：\")\n",
    "print(dataset)\n",
    "data=dataset.iloc[range(0,178),range(1,14)]\n",
    "target=dataset.iloc[range(0,178),range(0,1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78ea7f9e-6911-41a8-8f75-b2a7b6dbd163",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('seaborn-darkgrid')\n",
    "plt.rcParams['axes.unicode_minus']=False\n",
    "data.polt(kind='box',subplots=True,layout=(3,5),sharex=False,sharey=False)\n",
    "p=data.boxplot(return_type='dict')\n",
    "for i in range(13):\n",
    "    y=p['fliers'][i].get_ydata()\n",
    "    print('a',i+1,'中异常值',y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ce4c28a-651a-4ff1-80f5-afe98c2f2675",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn import preprocessing\n",
    "names=['label','a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','a11','a12','a13']\n",
    "dataset=pd.read_csv(\"wine.data\",names=names)\n",
    "data=dataset.iloc[range(0,178),range(1,14)]\n",
    "target=dataset.iloc[range(0,178),range(0,1)].values.reshape(1,178)[0]\n",
    "cdata=preprocessing.StandardScaler().fit_transfrom(data)\n",
    "print(cdata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e99e3a1-9942-48e5-af77-e1edb04ec85c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import cross_val_score\n",
    "x,y=cdata,target\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=0)\n",
    "k_range=range(1,15)\n",
    "k_error=[]\n",
    "for k in k_range:\n",
    "    model=KNeighborsClassifier(n_neighbors=k)\n",
    "    scores=cross_val_score(model,x,y,cv=5,scoring='accuracy')\n",
    "    k_error.append(1-scores.mean())\n",
    "plt.rcParams['font.sans-serif']='Simhei'\n",
    "plt.plot(k_range,k_error,'r-')\n",
    "plt.xlabel('k的取值')\n",
    "plt.ylabel('预测误差率')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ae73083-d75f-4d17-a4b5-6f27ec2ba672",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import cross_val_score\n",
    "model1=KNsighborsClassifier(n_neighbors=9)\n",
    "model.fit(x_train,y_train)\n",
    "pred=model.predict(x_test)\n",
    "ac=accuracy_score(y_test,pred)\n",
    "print(\"模型预测准确率：\",ac)\n",
    "print(\"测试集的预测标签：\",pred)\n",
    "print(\"测试集的真实标签：\",y_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
